<?php

/*	Vulpes - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 Alwin Garside

	This file is part of the Vulpes backend.

	The Vulpes backend is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	The Vulpes backend is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>. */

switch ($page->action) {

	case 'info':
		$client = new client(get('username'));
		$page->content .= '<client>' . xml_iterate($client) . $client->contracts() . '</client>';
		break;
		
	case 'insert_contract':
		$client = new client(get('username'));
		if (!$client->exists) {
			$page->error = $lang['client_doesnt_exist'];
			break;
		}
		$client->contract['new'] = new contract();
		$client->contract['new']->client_username = $client->username;
		$client->contract['new']->ip = get('new_ip');
		$client->contract['new']->mac = get('new_mac');
		$client->contract['new']->status = get('new_status');
		$client->contract['new']->plan_id = get('new_plan_id');
		$client->contract['new']->insert();
		iptables_refresh();
		$client = new client(get('username'));
		$page->content .= '<client>'.xml_iterate($client).$client->contracts().'</client>';
		break;

	case 'update_contract':
		$contract = new contract(get('contract_handle'));
		if (!$contract->exists)
			$page->error('{lang_contract_doesnt_exist}');
		$contract->ip = get('new_ip');
		$contract->mac = get('new_mac');
		if (get('new_status')) $contract->status = get('new_status');
		if (get('new_plan_id')) $contract->plan_id = get('new_plan_id');
		$contract->update();
		iptables_refresh();
		$client = new client(get('username'));
		$page->content .= '<client>'.xml_iterate($client).$client->contracts().'</client>';
		break;
		
	case 'refresh':
		iptables_refresh();
		break;
		
	case 'delete_contract':
		$contract = new contract(get('contract_handle'));
		$contract->delete();
		iptables_refresh();
		$client = new client(get('username'));
		$page->content .= '<client>'.xml_iterate($client).$client->contracts().'</client>';
		break;

	case 'insert':
		if (get('new_username')) {
			$client = new client(get('new_username'));
			if ($client->exists) {
				$page->error($lang['client_allready_exists']);
				break;
			}
			$client->username = get('new_username');
			$client->name = get('new_name');
			$client->address = get('new_address');
			$client->district = get('new_district');
			$client->postal_code = get('new_postal_code');
			$client->city = get('new_city');
			$client->state = get('new_state');
			$client->email = get('new_email');
			$client->upload = get('new_upload');
			$client->download = get('new_download');
			$client->set_password(get('new_password'));
			$client->insert();
			iptables_refresh();
		} else {
			$page->error('{lang_no_username_given}');		
		}
		break;

	case 'update':
		$client = new client(get('username'));
		if (get('new_username'))	$client->username = get('new_username');
		if (get('new_name'))		$client->name = get('new_name');
		if (get('new_address'))		$client->address = get('new_address');
		if (get('new_district'))	$client->district = get('new_district');
		if (get('new_postal_code'))	$client->postal_code = get('new_postal_code');
		if (get('new_city'))		$client->city = get('new_city');
		if (get('new_state'))		$client->state = get('new_state');
		if (get('new_email'))		$client->email = get('new_email');
		$client->update();
		iptables_refresh();
		break;
			
	case 'delete':
		$sql_delete_client = '
			DELETE FROM `clients`
			WHERE `username` = "' .get('username'). '"';
		$sql_delete_contracts = '
			DELETE FROM `contracts`
			WHERE `client_username` = "' .get('username').'"';
		if (!$db->query($sql_delete_client) || !$db->query($sql_delete_contracts)) {
			$page->error('{lang_client_not_deleted}');
		} else {
			$page->notify('{lang_client_deleted}');
			if (iptables_refresh())
				$page->notify('{lang_changes_applied}');
			else
				$page->error('{lang_changes_not_applied}');
		}
		break;
		
	case 'update_password':
		$client = new client(get('username'));
		if ($client->set_password(get('new_password')))
			$client->update();
		break;

}

$sql_clients = '
	SELECT `username`, `name`, `email`, `active`
	FROM `clients` ';
if (get('search'))
{
	$search = get('search');
	$sql_clients .= '
		WHERE `name` LIKE "%'.$search.'%"
		OR `username` LIKE "%'.$search.'%"';
}
$result_clients = $db->query($sql_clients);

$page->content .= '<clients>';
while ($client = $result_clients->fetch_object('client'))
{
	$page->content .= '<client>';
	foreach ($client as $key => $value)
	{
		if ($value)
			$page->content .= '<'.$key.'>' .$value. '</'.$key.'>';
	}
	$page->content .= '</client>';
}
$result_clients->free();
$page->content .= '</clients>';

$sql_plans = '
	SELECT *
	FROM `plans`';

$result_plans = $db->query($sql_plans);

$page->content .= '<plans>';
while ($plan = $result_plans->fetch_object()) {
	$page->content .= '<plan>';
	foreach ($plan as $key => $value) {
		$page->content .= '<'.$key.'>' .$value. '</'.$key.'>';
	}
	$page->content .= '</plan>';
}
$result_plans->free();
$page->content .= '</plans>';

?>
